package com.iotechn.unimall.admin.api.userShop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.*;
import com.iotechn.unimall.data.mapper.*;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class AdminUserShopServiceImpl implements AdminUserShopService {
    @Autowired
    private UserShopMapper userShopMapper;
    @Autowired
    private ShopUserMapper shopUserMapper;
     @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private ShopBiaozhunMapper biaozhunMapper;

    @Override
    public Page<UserShopDO> list(Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<UserShopDO> wrapper = new EntityWrapper<UserShopDO>();
        List<Long> roleIds=SessionUtil.getAdmin().getRoleIds();
        if(roleIds==null || roleIds.size()==0){
            List<UserShopDO> UserShopDOS =new ArrayList<>();
            return new Page<>(UserShopDOS, page, limit, 0);
        }
        if (roleIds.get(0).intValue()==2) {
            wrapper.eq("xiangmujingliid", adminId);
        }else if (roleIds.get(0).intValue()==3) {
            wrapper.eq("fuzerenid", adminId);
        }else if (roleIds.get(0).intValue()==4) {
            wrapper.eq("jianlizongjian_id", adminId);
        }else if (roleIds.get(0).intValue()==5) {
            wrapper.eq("jianli_id", adminId);
        }else if (roleIds.get(0).intValue()==1) {
         } else {
            wrapper.eq("user_id", adminId);
        }
         wrapper.orderBy("id", false);
        Integer count = userShopMapper.selectCount(wrapper);
        List<UserShopDO> UserShopDOS = userShopMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(UserShopDOS, page, limit, count);
    }
   @Override
    public Page<UserShopDO> listFront(Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<UserShopDO> wrapper = new EntityWrapper<UserShopDO>();
        //根据权限判断是项目经理还是监理 监理查询监理列表，项目经理查看项目经理列表
         List<Long> roleIds=SessionUtil.getAdmin().getRoleIds();
         if(roleIds==null || roleIds.size()==0){
             List<UserShopDO> UserShopDOS =new ArrayList<>();
             return new Page<>(UserShopDOS, page, limit, 0);
         }
        if (roleIds.get(0).intValue()==2) {
            wrapper.eq("xiangmujingliid", adminId);
        }else if (roleIds.get(0).intValue()==3) {
           wrapper.eq("fuzerenid", adminId);
       }else if (roleIds.get(0).intValue()==4) {
           wrapper.eq("jianlizongjian_id", adminId);
       }else if (roleIds.get(0).intValue()==5) {
           wrapper.eq("jianli_id", adminId);
       }else if (roleIds.get(0).intValue()==1) {
            //wrapper.eq("jianli_id", adminId);
        } else {
            wrapper.eq("user_id", adminId);
       }
        wrapper.orderBy("id", false);
        Integer count = userShopMapper.selectCount(wrapper);
        List<UserShopDO> UserShopDOS = userShopMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        if(UserShopDOS!=null && UserShopDOS.size()>0){
            for(UserShopDO userShopDO:UserShopDOS){
                AdminDO adminDOshigong=adminMapper.selectById(userShopDO.getFuzerenid());
                userShopDO.setShigongPhone(adminDOshigong.getPhone());
                AdminDO adminDOjianli=adminMapper.selectById(userShopDO.getJianliId());
                userShopDO.setJianliPhone(adminDOjianli.getPhone());
            }
        }
        return new Page<>(UserShopDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserShopDO create(UserShopDO userShopDO, Long adminId) throws ServiceException {
        Date now = new Date();
        userShopDO.setGmtCreate(now);
        userShopDO.setAdminId(adminId);
        userShopDO.setTrueName(SessionUtil.getAdmin().getRealname());
        userShopDO.setGmtUpdate(now);
        if(userShopDO.getAreaId()!=null){
            AreaDO areaDO=  areaMapper.selectById(userShopDO.getAreaId());
            userShopDO.setArea(areaDO.getAreaName());
        }
        userShopDO.setShopName(userShopDO.getArea()+userShopDO.getPianarea()+userShopDO.getBiaoduan());

        if(userShopDO.getXiangmujingliid()!=null){
            AdminDO adminDO=  adminMapper.selectById(userShopDO.getXiangmujingliid());
            userShopDO.setXiangmujingliname(adminDO.getRealname());
         }
        if(userShopDO.getFuzerenid()!=null){
            AdminDO adminDO=  adminMapper.selectById(userShopDO.getFuzerenid());
            userShopDO.setFuzerenname(adminDO.getRealname());
            userShopDO.setShigongPhone(adminDO.getPhone());
        }
        if(userShopDO.getJianlizongjianId()!=null){
            AdminDO adminDO=  adminMapper.selectById(userShopDO.getJianlizongjianId());
            userShopDO.setJianlizongjianName(adminDO.getRealname());
        }
        if(userShopDO.getJianliId()!=null){
            AdminDO adminDO=  adminMapper.selectById(userShopDO.getJianliId());
            userShopDO.setJianliname(adminDO.getRealname());
        }

        if (userShopMapper.insert(userShopDO) > 0) {
            userShopDO.setId(userShopDO.getId());

            shopUserMapper.batchAdd(addShopList(userShopDO));
            return userShopDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }
    public  List<ShopUserDO> addShopList(UserShopDO userShopDO){
        List<ShopUserDO> list=new ArrayList();

        if(userShopDO.getXiangmujingliid()!=null){
              list.add(addShopUser(userShopDO.getId(),2,userShopDO.getXiangmujingliid(),userShopDO.getAdminId()));
        }
        if(userShopDO.getFuzerenid()!=null){
            list.add(addShopUser(userShopDO.getId(),3,userShopDO.getFuzerenid(),userShopDO.getAdminId()));

        }
        if(userShopDO.getJianlizongjianId()!=null){
            list.add(addShopUser(userShopDO.getId(),4,userShopDO.getJianlizongjianId(),userShopDO.getAdminId()));

        }
        if(userShopDO.getJianliId()!=null){
            list.add(addShopUser(userShopDO.getId(),5,userShopDO.getJianliId(),userShopDO.getAdminId()));
        }
        return list;
    }
    public ShopUserDO addShopUser(Long shopId,Integer userType,Long userId,Long adminId){
        Date now = new Date();
        ShopUserDO shopUserDO=new ShopUserDO();
        shopUserDO.setShopId(shopId);
        shopUserDO.setGmtCreate(now);
        shopUserDO.setGmtUpdate(now);
        shopUserDO.setUserType(userType);
        shopUserDO.setAdminId(adminId);
        shopUserDO.setUserId(userId);
        return shopUserDO;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(UserShopDO userShopDO, Long adminId) throws ServiceException {
        Long id = userShopDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        if(userShopDO.getAreaId()!=null){
            AreaDO areaDO=  areaMapper.selectById(userShopDO.getAreaId());
            userShopDO.setArea(areaDO.getAreaName());
        }
        userShopDO.setShopName(userShopDO.getArea()+userShopDO.getPianarea()+userShopDO.getBiaoduan());

        if(userShopDO.getXiangmujingliid()!=null){
            AdminDO adminDO=  adminMapper.selectById(userShopDO.getXiangmujingliid());
            userShopDO.setXiangmujingliname(adminDO.getRealname());
        }
        if(userShopDO.getFuzerenid()!=null){
            AdminDO adminDO=  adminMapper.selectById(userShopDO.getFuzerenid());
            userShopDO.setFuzerenname(adminDO.getRealname());
        }
        if(userShopDO.getJianlizongjianId()!=null){
            AdminDO adminDO=  adminMapper.selectById(userShopDO.getJianlizongjianId());
            userShopDO.setJianlizongjianName(adminDO.getRealname());
        }
        if(userShopDO.getJianliId()!=null){
            AdminDO adminDO=  adminMapper.selectById(userShopDO.getJianliId());
            userShopDO.setJianliname(adminDO.getRealname());
        }
        userShopDO.setGmtUpdate(now);
        if (userShopMapper.updateById(userShopDO) > 0) {
           // ShopUserDO shopUserDO=new ShopUserDO();
            shopUserMapper.delete(new EntityWrapper<ShopUserDO>()
                    .eq("shop_id", userShopDO.getId()));
            shopUserMapper.batchAdd(addShopList(userShopDO));
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (userShopMapper.deleteById(id) > 0) {
            Map<String, Object> var1 =new HashMap<>();
            var1.put("shop_id",id);
            spuMapper.deleteByMap(var1);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public UserShopDO detail(Long id, Long adminId) throws ServiceException {
        UserShopDO userShopDO= userShopMapper.selectById(id);
            AdminDO adminDOshigong=adminMapper.selectById(userShopDO.getFuzerenid());
            userShopDO.setShigongPhone(adminDOshigong.getPhone());
            AdminDO adminDOjianli=adminMapper.selectById(userShopDO.getJianliId());
            userShopDO.setJianliPhone(adminDOjianli.getPhone());
        return userShopDO;
    }
    @Override
    public UserShopDO qrCodeDetail(String qrCode) throws ServiceException {
        Wrapper<UserShopDO> wrapper = new EntityWrapper<UserShopDO>();
        wrapper.eq("shop_qrcode",qrCode);
        UserShopDO userShopDO=userShopMapper.selectList(wrapper).get(0);
        if(userShopDO!=null){
            if(userShopDO.getAreaId()!=null){
                AreaDO areaDO=  areaMapper.selectById(userShopDO.getAreaId());
                userShopDO.setArea(areaDO.getAreaName());
            }
            if(userShopDO.getXiangmujingliid()!=null){
                AdminDO adminDO=  adminMapper.selectById(userShopDO.getXiangmujingliid());
                userShopDO.setXiangmujingliname(adminDO.getRealname());
                userShopDO.setXiangmujingliPhone(adminDO.getPhone());
             }
            if(userShopDO.getFuzerenid()!=null){
                AdminDO adminDO=  adminMapper.selectById(userShopDO.getFuzerenid());
                userShopDO.setFuzerenname(adminDO.getRealname());
                userShopDO.setShigongPhone(adminDO.getPhone());

            }
            if(userShopDO.getJianlizongjianId()!=null){
                AdminDO adminDO=  adminMapper.selectById(userShopDO.getJianlizongjianId());
                userShopDO.setJianlizongjianName(adminDO.getRealname());
                userShopDO.setJianlizongjianPhone(adminDO.getPhone());
            }
            if(userShopDO.getJianliId()!=null){
                AdminDO adminDO=  adminMapper.selectById(userShopDO.getJianliId());
                userShopDO.setJianliname(adminDO.getRealname());
                userShopDO.setJianliPhone(adminDO.getPhone());
            }
            /*Wrapper<SpuDO> spuDOWrapper = new EntityWrapper<SpuDO>();
            spuDOWrapper.eq("shop_id",userShopDO.getId());
            List<SpuDO> list=spuMapper.selectList(spuDOWrapper);
            userShopDO.setSpuDOList(list);
*/
            Wrapper<ShopBiaozhunDO> biaozhunDOWrapper = new EntityWrapper<ShopBiaozhunDO>();
            List<String> orderAsc = new ArrayList<>();
            orderAsc.add("gongchengPxuhao");
            orderAsc.add("gongcheng_xuhao");
            wrapper.orderAsc(orderAsc);
            biaozhunDOWrapper.eq("shop_id",userShopDO.getId()).orderAsc(orderAsc);
            List<ShopBiaozhunDO> biaozhunDOList=biaozhunMapper.selectList(biaozhunDOWrapper);
            userShopDO.setBiaozhunDOList(biaozhunDOList);
        }

        return userShopDO;
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, String reason, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        UserShopDO userShopDO = new UserShopDO();
        userShopDO.setId(id);
        userShopDO.setStatus(status);
        userShopDO.setShopRefuseReson(reason);
        userShopDO.setGmtUpdate(new Date());
        if (userShopMapper.updateById(userShopDO) > 0) {
            return true;
        }
        return false;
    }


    @Override
    public Boolean updateIdentityStatus(Long adminId, Long id, String reason, Integer status) throws ServiceException {
        if (status == null || (status != 2 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        UserShopDO userShopDO = new UserShopDO();
        userShopDO.setId(id);

        userShopDO.setGmtUpdate(new Date());
        if (userShopMapper.updateById(userShopDO) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateBusinessStatus(Long adminId, Long id, String reason, Integer status) throws ServiceException {
        if (status == null || (status != 2 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        UserShopDO userShopDO = new UserShopDO();
        userShopDO.setId(id);
         userShopDO.setGmtUpdate(new Date());
        if (userShopMapper.updateById(userShopDO) > 0) {
            return true;
        }
        return false;
    }


}